package com.snatch.snatchmgr.nodemanage;

import java.io.StringReader;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;

import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.snatch.common.vo.Config;
import com.snatch.common.vo.ConfigModifyOutput;
import com.snatch.common.vo.NodeManageInput;
import com.snatch.common.vo.NodeManageOutput;
import com.snatch.common.vo.NodeTaskInput;
import com.snatch.common.vo.NodeTaskOutput;
import com.snatch.common.vo.NodeWeb;
import com.snatch.common.vo.WebConfig;
import com.snatch.ice.ConnectionParam;
import com.snatch.ice.IceClient;
import com.snatch.snatchmgr.constant.SystemConstant;
import com.snatch.snatchmgr.system.SystemData;
import com.snatch.snatchmgr.vo.WebSnatchInfo;
import com.snatch.snatchnd.zreocsrvi.NodeZreocSrvIPrx;
import com.snatch.snatchnd.zreocsrvi.NodeZreocSrvIPrxHelper;
import com.thoughtworks.xstream.XStream;

/**
* @ClassName: NodeManageIntegration
* @Description: TODO(这里用一句话描述这个类的作用)
*
*/
@Component
public class NodeManageIntegration {
	private Map<String, NodeZreocSrvIPrx> nodeZreocSrvIPrxs = new ConcurrentHashMap<String, NodeZreocSrvIPrx>();
	private Map<String, IceClient> iceClients = new ConcurrentHashMap<String, IceClient>();
	
	private String objKey = "NodeZreocSrv";
	

	@SuppressWarnings("rawtypes")
	public void addIceClient(String ip, String onlyCode) {
		try {
		IceClient iceClient = iceClients.get(onlyCode);
		if (iceClient == null) {
			iceClient = new IceClient();
		}else {
			iceClient.destoryProxy();
		}
		ConnectionParam connParam = new ConnectionParam();
		connParam.setClientCfgClassPath(SystemConstant.ICECONFG_CLIENT);
		connParam.setConnType(ConnectionParam.TCP);
		connParam.setIp(ip);
		connParam.setPort("10004");
		connParam.setTimeOut(-1);

		Map<String, Class> prxHelperMap = new HashMap<String, Class>();
		prxHelperMap.put(objKey, NodeZreocSrvIPrxHelper.class);

		iceClient.initClientPrxs(connParam, prxHelperMap);
		NodeZreocSrvIPrx nodeZreocSrvIPrx = (NodeZreocSrvIPrx) iceClient.getPrxByKey(objKey);
		nodeZreocSrvIPrxs.put(onlyCode, nodeZreocSrvIPrx);
		iceClient.setAvailable(true);
		
		} catch (Exception e) {
			if (e instanceof Ice.SocketException) {
				IceClient iceClient = iceClients.get(onlyCode);
				if (iceClient != null) {
					iceClient.setAvailable(false);
					iceClient.destoryProxy();
				}
			}
		}
	}

	/**
	 * 开启或者停止节点纯线程的数据抓取
	 * 
	 * @param onlyCode
	 * @param nodeManageInput
	 * @return
	 *//*
	public NodeManageOutput nodeManage(String onlyCode, NodeManageInput nodeManageInput) {
		NodeManageOutput nodeManageOutput = new NodeManageOutput();
		NodeZreocSrvIPrx nodeZreocSrvIPrx = null;
		XStream xs = new XStream();
		StringWriter sw = new StringWriter();
		xs.alias("NodeManageInput", NodeManageInput.class);
		xs.toXML(nodeManageInput, sw);
		nodeZreocSrvIPrx = nodeZreocSrvIPrxs.get(onlyCode);
		StringReader reader = new StringReader(nodeZreocSrvIPrx.nodeManage(sw.toString()));
		xs.alias("NodeManageOutput", NodeManageOutput.class);
		try {
			xs.fromXML(reader, nodeManageOutput);
		} catch (Exception e) {
			e.printStackTrace();
			nodeManageOutput.setStatus(0);
			nodeManageOutput.setRemark("数据格式出错");
			if (e instanceof Ice.SocketException) {
				IceClient iceClient = iceClients.get(onlyCode);
				if (iceClient != null) {
					iceClient.setAvailable(false);
					iceClient.destoryProxy();
				}
			} 
		}
		return nodeManageOutput;
	}
	*/
	
	
	
	public NodeManageOutput nodeManage(NodeManageInput nodeManageInput) {
		NodeManageOutput nodeManageOutput = new NodeManageOutput();
		NodeZreocSrvIPrx nodeZreocSrvIPrx = nodeZreocSrvIPrxs.get(nodeManageInput.getOnlyCode());
		try {
			String stru = JSON.toJSONString(nodeManageInput);
			String rtru = nodeZreocSrvIPrx.nodeManage(stru);
			nodeManageOutput  = (NodeManageOutput)JSON.parseObject(rtru,NodeManageOutput.class);
		} catch (Exception e) {
			e.printStackTrace();
			nodeManageOutput.setStatus(0);
			nodeManageOutput.setRemark("数据格式出错");
			if (e instanceof Ice.SocketException) {
				IceClient iceClient = iceClients.get(nodeManageInput.getOnlyCode());
				if (iceClient != null) {
					iceClient.setAvailable(false);
					iceClient.destoryProxy();
				}
				SystemData.lostNode(nodeManageInput.getOnlyCode());
			} 
		}
		return nodeManageOutput;
	}
	
	public NodeTaskOutput snatchTask(NodeTaskInput nodeTaskInput){
		long time = System.currentTimeMillis();
		NodeTaskOutput nodeTaskOutput = new NodeTaskOutput();
		NodeZreocSrvIPrx nodeZreocSrvIPrx = nodeZreocSrvIPrxs.get(nodeTaskInput.getOnlyCode());
		try {
			String stru = JSON.toJSONString(nodeTaskInput);
			String rtru = nodeZreocSrvIPrx.snatchTask(stru);
			nodeTaskOutput  = (NodeTaskOutput)JSON.parseObject(rtru,NodeTaskOutput.class);
			System.out.println("ice 时间打印："+(System.currentTimeMillis()-time));
		} catch (Exception e) {
			e.printStackTrace();
			nodeTaskOutput.setStatus(0);
			nodeTaskOutput.setRemark("数据格式出错");
			if (e instanceof Ice.SocketException) {
				IceClient iceClient = iceClients.get(nodeTaskInput.getOnlyCode());
				if (iceClient != null) {
					iceClient.setAvailable(false);
					iceClient.destoryProxy();
				}
				SystemData.lostNode(nodeTaskInput.getOnlyCode());
			} 
		}
		return nodeTaskOutput;
	}
	
	
	

	/**
	 * 修改节点配置信息
	 * 
	 * @param onlyCode
	 * @param config
	 * @return
	 */
	public ConfigModifyOutput configModify(String onlyCode, Config config) {
		ConfigModifyOutput configModifyOutput = new ConfigModifyOutput();
		NodeZreocSrvIPrx nodeZreocSrvIPrx = null;
		XStream xs = new XStream();
		StringWriter sw = new StringWriter();
		xs.alias("Config", Config.class);
		xs.toXML(config, sw);
		nodeZreocSrvIPrx = nodeZreocSrvIPrxs.get(onlyCode);
		StringReader reader = new StringReader(nodeZreocSrvIPrx.configModify(sw.toString()));
		xs.alias("ConfigModifyOutput", ConfigModifyOutput.class);
		try {
			xs.fromXML(reader, configModifyOutput);
		} catch (Exception e) {
			e.printStackTrace();
			configModifyOutput.setStatus(0);
			configModifyOutput.setRemark("数据格式出错");
			if (e instanceof Ice.SocketException) {
				IceClient iceClient = iceClients.get(onlyCode);
				if (iceClient != null) {
					iceClient.setAvailable(false);
					iceClient.destoryProxy();
					
				}
				SystemData.lostNode(onlyCode);
			} 
		}
		return configModifyOutput;
	}
	

	/**
	 * 销毁ICE客户端
	 */
	public void destoryAllIceClient() {
		Set<Map.Entry<String, IceClient>> entryseSet = iceClients.entrySet();
		for (Map.Entry<String, IceClient> entry : entryseSet) {
			entry.getValue().setAvailable(false);
			entry.getValue().destoryProxy();
		}
	}
	
	
	
	
	

	
}
